package se.statifyme.client.forms;

import se.statifyme.client.record.RecordClient;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class SaveForm extends Composite {

	interface Binder extends UiBinder<Widget, SaveForm> {}
	
	@UiField
	HasText serviceField;
	@UiField
	HasText idField;
	@UiField
	HasText jsonField;
	@UiField
	Button saveButton;
	@UiField
	HasText serviceURIField;
	@UiField
	HasText statusField;
	
	private final SaveCallback saveCallback;
	
	public SaveForm() {
		initWidget(GWT.<Binder>create(Binder.class).createAndBindUi(this));
		saveCallback = new SaveCallback();
		updateServiceURI();
	}
	
	@UiHandler(value = {"serviceField", "idField", "jsonField"})
	void onSubmit(KeyUpEvent event) {
		boolean submit = false;
		if (event.getSource() instanceof TextArea) {
			submit = event.isControlKeyDown() && event.getNativeKeyCode() == KeyCodes.KEY_ENTER;
		} else {
			submit = event.getNativeKeyCode() == KeyCodes.KEY_ENTER;
		}
		if (submit) {
			executeSave();
		}
	}
	
	@UiHandler(value = "saveButton")
	void onSave(ClickEvent event) {
		executeSave();
	}

	private void executeSave() {
		setEnabled(false);
		String url = buildURI();
		String data = jsonField.getText();
		RecordClient.INSTANCE.post(url, data, saveCallback);
	}
	
	@UiHandler(value = {"serviceField", "idField"})
	void onURIChanged(KeyUpEvent event) {
		updateServiceURI();
	}
	
	private void updateServiceURI() {
		serviceURIField.setText("POST @ " + buildURI());
	}
	
	private String buildURI() {
		String service = serviceField.getText();
		String id = idField.getText();
		return RecordClient.buildServiceRecordURI(service, id);
	}
	
	private void setEnabled(boolean enabled) {
		setEnabled(serviceField, enabled);
		setEnabled(idField, enabled);
		setEnabled(jsonField, enabled);
		setEnabled(saveButton, enabled);
		setEnabled(statusField, enabled);
	}
	
	private void setEnabled(Object o, boolean enabled) {
		if (o instanceof HasEnabled) {
			((HasEnabled) o).setEnabled(enabled);
		}
	}

	private final class SaveCallback implements AsyncCallback<String> {
		@Override
		public void onFailure(Throwable caught) {
			statusField.setText("ERROR: " + caught.getMessage());
			setEnabled(true);
		}

		@Override
		public void onSuccess(String result) {
			statusField.setText("Item saved.");
			setEnabled(true);
		}
	}
	
}
